package bb.lanxing.calc.notify;

import android.speech.tts.TextToSpeech;

import com.github.mikephil.charting.utils.Utils;
import bb.lanxing.App;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.debug.FileLogger;
import java.util.Locale;

public class TFTTSChecker {
    private double lastTtsDistance;
    private long lastTtsDuration;
    private FileLogger logger;
    private boolean ttsEnable;
    private boolean ttsGPSEnable;
    private int ttsIntervalDistance = 0;
    private int ttsIntervalDuration = 0;

    private TextToSpeech textToSpeech;

    private void log(String msg) {
    }

    public TFTTSChecker() {
        initTTSIfNeed();
    }

    private void initTTSIfNeed() {
        textToSpeech = new TextToSpeech(App.getContext(), status -> {
            if (status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE);
                textToSpeech.setSpeechRate(0.5f);
            }
        });
    }

    public void release() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        FileLogger fileLogger = this.logger;
        if (fileLogger != null) {
            fileLogger.destroy();
        }
    }

    public void updateConfigs(boolean ttsEnable, boolean ttsGPSEnable,
                              int ttsIntervalDistance, int ttsIntervalDuration) {
        this.ttsEnable = ttsEnable;
        this.ttsGPSEnable = ttsGPSEnable;
        this.ttsIntervalDistance = ttsIntervalDistance;
        this.ttsIntervalDuration = ttsIntervalDuration;
        initTTSIfNeed();
    }

    public void checkTtsNotifyByDistance(Workout workout) {
        if (!this.ttsEnable || workout == null || this.ttsIntervalDistance == 0) {
            return;
        }
        try {
            if (workout.getDistance() <= Utils.DOUBLE_EPSILON || !checkDistance(workout, this.ttsIntervalDistance)) {
                return;
            }
            ttsSpeak(workout);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean checkDistance(Workout workout, int meters) {
        int distance = (int) workout.getDistance();
        if ((distance - ((int) this.lastTtsDistance)) / meters >= 1) {
            this.lastTtsDistance = distance;
            return true;
        }
        return false;
    }

    public void checkTtsNotifyByDuration(Workout workout) {
        if (!this.ttsEnable || workout == null || this.ttsIntervalDuration == 0) {
            return;
        }
        long duration = workout.getDuration();
        if (duration > 0) {
            try {
                if (checkDuration(duration, this.ttsIntervalDuration)) {
                    ttsSpeak(workout);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void notifyGPSReceived() {
        if (this.ttsGPSEnable) {
            VibratorHelper.vibrate(App.getContext(), VibratorHelper.VIBRATE_GPS_RECEIVED_PATTERN);
            try {
                speak("已获得GPS信号");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void notifyGPSLost() {
        if (this.ttsGPSEnable) {
            VibratorHelper.vibrate(App.getContext(), VibratorHelper.VIBRATE_GPS_LOST_PATTERN);
            try {
                speak("GPS信号丢失");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private boolean checkDuration(long duration, int interval) {
        if ((duration - this.lastTtsDuration) / interval >= 1) {
            this.lastTtsDuration = duration;
            return true;
        }
        return false;
    }

    private void ttsSpeak(Workout workout) {
        String format;
        if (workout == null) {
            speak("尚未开始记录");
            return;
        }
        double distance = workout.getDistance();
        if (workout.getDuration() == 0) {
            log("tts speak duration = 0");
            speak(String.format(Locale.CHINA, "运动距离 %.1f公里，时间 0分钟",
                    CommonUtil.round(distance / 1000.0d, 1)));
            return;
        }
        long duration = workout.getDuration();
        long j = duration >= 60 ? duration % 60 : duration;
        long j2 = duration / 60;
        long j3 = j2 >= 60 ? j2 % 60 : j2;
        long j4 = j2 / 60;
        if (workout.getSport() != Workout.TYPE_RUNNING || j <= 0) {
            if (j4 != 0) {
                format = String.format(Locale.CHINA, "%d小时%d分钟", j4, j3);
            } else {
                format = String.format(Locale.CHINA, "%d分钟", j3);
            }
        } else if (j4 != 0) {
            format = String.format(Locale.CHINA, "%d小时%d分%s秒", j4, j3, j);
        } else if (j3 != 0) {
            format = String.format(Locale.CHINA, "%d分%d秒", j3, j);
        } else {
            format = String.format(Locale.CHINA, "%d秒", j);
        }
        speak(String.format(Locale.CHINA, "距离 %.1f公里，时间 %s，均速 %.1f公里每小时",
                CommonUtil.round(distance / 1000.0d, 1), format,
                CommonUtil.round((distance / workout.getDuration()) * 3.6d, 1)));
        log("tts speak: duration = " + format + ", distance = " + distance);
    }

    public void speak(final String inputText) {
        if (textToSpeech != null) {
            textToSpeech.speak(inputText, TextToSpeech.QUEUE_FLUSH, null, null);
        }
    }
}